﻿using B.Rabbit.DataBase.Domain;
using B.Rabbit.DataBase.Infrastructure.Infrastructure;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace B.Rabbit.DataBase.Infrastructure.Implement
{
    /// <summary>
    /// 仓储
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BaseRepository<T> : IBaseRepository<T> where T : BaseEntity
    {
        MyDbContext db;

        public BaseRepository(MyDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(T t)
        {
            await db.Set<T>().AddAsync(t);
            return await db.SaveChangesAsync();
        }
        /// <summary>
        /// 批量添加
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> AddRange(List<T> t)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 列表查询
        /// </summary>
        /// <returns></returns>
        public  IQueryable<T> GetList()
        {
            return  db.Set<T>();
        }
        /// <summary>
        /// 单条件查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetValue(long id)
        {
            return await db.Set<T>().FindAsync(id);
        }
        /// <summary>
        /// 受影响行数（只执行一次）
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> SaveChanges()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T t)
        {
            db.Set<T>().Update(t);
            return await db.SaveChangesAsync();
        }
    }
}
